package com.itheima.web;

import com.itheima.domain.User;
import com.itheima.service.Service;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            //设置编码
        request.setCharacterEncoding("utf-8");
        //得到username和password
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //将username和password封装到user对象中
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        //调用login方法
        Service service = new Service();
        User user1 = service.login(user);

        //判断
        if(user1==null){
            request.getRequestDispatcher("fileServlet").forward(request,response);
        }else {
            //保存数据
            request.setAttribute("user",user1);
            request.getRequestDispatcher("successServlet").forward(request,response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
